from sqlalchemy import BigInteger, False_, ForeignKey, String
from sqlalchemy.orm import Mapped, mapped_column, relationship

from poker_bot.core.models.enums.user import LangCode, UserRole

from .base import TimedBaseModel


class User(TimedBaseModel):
    __tablename__ = "user"
    __mapper_args__ = {"eager_defaults": True}

    id: Mapped[int] = mapped_column(primary_key=True)
    role: Mapped[UserRole] = mapped_column(server_default="user")
    telegram_id: Mapped[int] = mapped_column(BigInteger, unique=True)
    username: Mapped[str] = mapped_column(String(255))
    accounts = relationship("Account", back_populates="user", cascade="all,delete")
    selected_account_id: Mapped[int | None]
    is_subscribed: Mapped[bool] = mapped_column(server_default=False_())
    lang_code: Mapped[LangCode] = mapped_column(server_default="ru")
    deposits = relationship("Deposit", back_populates="user", cascade="all,delete")
    withdrawals = relationship("Withdraw", back_populates="user", cascade="all,delete")
    referrer_id = mapped_column(ForeignKey("user.id"))
    referrals = relationship(
        "User",
        back_populates="referrer",
    )
    referrer = relationship("User", back_populates="referrals", remote_side=[id])

    def __repr__(self):
        return f"<Пользователь @{self.username} {self.telegram_id}>"

    def __str__(self):
        return self.__repr__()
